package thread;

/**
 * 守护线程
 *
 * java进程中至少要有一条用户线程活着,否则进程就会结束.如果进程在结束时发现还有守护线程活着会强行杀死这些
 * 线程.
 *
 * 守护线程是通过用户线程启动前调用方法setDaemon(true)设置而来的.
 * 而用户线程就是我们默认创建的线程.
 *
 */
public class DaemonThreadDemo {
    public static void main(String[] args) {
        Thread rose = new Thread("rose"){
          public void run(){
              for (int i = 0; i < 5; i++) {
                  System.out.println(getName()+":let me go!");
                  try {
                      Thread.sleep(2000);
                  } catch (InterruptedException e) {
                      e.printStackTrace();
                  }
              }
              System.out.println(getName()+":啊啊啊啊啊啊啊啊啊aaaaaaaaAAAAAAAA");
              System.out.println("噗通");
          }
        };

        Thread jack = new Thread("jack"){
          public void run(){
              while (true){
                  System.out.println(getName()+":you jump! i jump!");
                  try {
                      Thread.sleep(2000);
                  } catch (InterruptedException e) {
                      e.printStackTrace();
                  }
              }
          }
        };


        rose.start();
        jack.setDaemon(true);
        jack.start();

        while (true);
    }
}
